<?php
declare (strict_types = 1);

namespace app\model;

use think\facade\Log;
use think\Model;
use think\model\concern\SoftDelete;

/**
 * @mixin Model
 */
class User extends Model
{
    use SoftDelete;

    protected $table = 'think_user';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';//创建时间
    protected $updateTime = 'update_time';//更新时间
    protected string $deleteTime = 'delete_time';//删除时间
    protected $type = [
        'id' => 'integer',
        'state' => 'integer',
        'sex' => 'integer',
        'age' => 'integer',
    ];

    /**
     * 状态 0: 风险
     */
    public const STATE_RISK = 0;

    /**
     * 状态 1: 正常
     */
    public const STATE_NORMAL = 1;

    /**
     * 状态 2: 限制
     */
    public const STATE_LIMIT = 2;

    /**
     * 状态列表
     */
    public const STATE_LIST = [
        self::STATE_RISK => '风险',
        self::STATE_NORMAL => '正常',
        self::STATE_LIMIT => '限制',
    ];

    /**
     * 设置标签
     *
     * @param $value
     *
     * @return string
     */
    public function setTagsAttr($value): string
    {
        return implode(',', $value);
    }

    /**
     * 获取标签
     *
     * @param $value
     *
     * @return array
     */
    public function getTagsAttr($value): array
    {
        if (empty($value)) {
            return [];
        }
        return array_map(static function ($item) {
            return (int)$item;
        }, explode(',' , $value));
    }

    /**
     * 设置地区
     *
     * @param $value
     *
     * @return string
     */
    public function setAreaAttr($value): string
    {
        if (empty($value)) {
            return '';
        }
        return implode(',' , $value);
    }

    /**
     * 获取地区
     *
     * @param $value
     *
     * @return array
     */
    public function getAreaAttr($value): array
    {
        if (empty($value)) {
            return [];
        }
        return array_map(static function ($item) {
            return (int)$item;
        }, explode(',' , $value));
    }

    /**
     * 设置性别
     * @param $value
     *
     * @return int
     */
    public function setSexAttr($value): int
    {
        return (int)$value;
    }

    /**
     * 获取性别
     * @param $value
     *
     * @return bool
     */
    public function getSexAttr($value): bool
    {
        return (bool)$value;
    }

}
